home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
12984
/
12984.xpi
/
chrome
/
VideoDownloaderToolbar.jar
/
content
/
netmonitor.js
< prev
next >
Wrap
Text File
|
2010-01-29
|
9KB
|
305 lines
if(!com) var com={};
if(!com.VidBar) com.VidBar={};
com.VidBar.NetMonitor = function(repository, pref) {
try {
this.repository = repository;
this.pref = pref;
this.pref.registerObserver(["mediareq-extensions",
"mediaweight-enabled", "mediaweight-threshold"], this);
this.observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
this.observerService.addObserver(this, "http-on-modify-request", false);
this.observerService.addObserver(this, "http-on-examine-response",
false);
this.observerService.addObserver(this, "quit-application", false);
this.cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
.getService(Components.interfaces.nsICacheService);
this.httpCacheSession = this.cacheService.createSession("HTTP",
Components.interfaces.nsICache.STORE_ANYWHERE,
Components.interfaces.nsICache.STREAM_BASED);
this.httpCacheSession.doomEntriesIfExpired = false;
this.updateReqExtensions();
this.updateMediaWeight();
this.typePattern = new RegExp("^(audio|video)/");
} catch (e) {
com.VidBar.__e("com.VidBar.NetMonitor.constructor: " + e);
}
}
com.VidBar.NetMonitor.prototype = {
observe : function(subject, topic, data) {
if (topic == "quit-application") {
this.observerService.removeObserver(this, "http-on-modify-request");
this.observerService.removeObserver(this,
"http-on-examine-response");
this.observerService.removeObserver(this, "quit-application");
} else if (topic == "http-on-modify-request") {
if (typeof Components == 'undefined')
return;
this.monitorRequest(subject);
} else if (topic == "http-on-examine-response") {
if (typeof Components == 'undefined')
return;
this.monitorResponse(subject);
}
},
observePref : function(data) {
if (data == "mediareq-extensions")
this.updateReqExtensions();
if (data == "mediaweight-enabled" || data == "mediaweight-threshold")
this.updateMediaWeight();
},
updateReqExtensions : function() {
com.VidBar.__d("com.VidBar.NetMonitor.updateReqExtensions");
var exts = this.pref.getMediaExtensions();
this.reqPattern = new RegExp("[/\\?&]([^/\\?&=]+\\.(" + exts
+ "))(?:$|\\?|&|/)");
},
updateMediaWeight : function() {
com.VidBar.__d("com.VidBar.NetMonitor.updateMediaWeight");
this.minMediaSize = this.pref.getMinMediaSize();
},
getWindowFromChannel : function(httpChannel) {
//com.VidBar.__d("com.VidBar.NetMonitor.getWindowFromChannel");
var wnd = null;
if (httpChannel.notificationCallbacks) {
try {
var notif = httpChannel.notificationCallbacks
.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
wnd = notif.getInterface(Components.interfaces.nsIDOMWindow);
} catch (e) {
}
}
if (wnd == null && httpChannel.loadGroup
&& httpChannel.loadGroup.notificationCallbacks) {
try {
var lgNotif = httpChannel.loadGroup.notificationCallbacks
.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
wnd = lgNotif.getInterface(Components.interfaces.nsIDOMWindow);
} catch (e) {
}
}
return wnd;
},
monitorRequest : function(subject) {
//com.VidBar.__d("com.VidBar.NetMonitor.monitorRequest");
try {
var channel = subject
.QueryInterface(Components.interfaces.nsIHttpChannel);
if (channel.requestMethod != "GET")
return;
var request = subject
.QueryInterface(Components.interfaces.nsIRequest);
var url = request.name;
//com.VidBar.__d("com.VidBar.NetMonitor requst: "+url);
var cacheEntryDescriptor = this.httpCacheSession.openCacheEntry(
url, Components.interfaces.nsICache.ACCESS_READ, false);
if (cacheEntryDescriptor
&& (cacheEntryDescriptor.accessGranted & 1)) {
var headers = cacheEntryDescriptor
.getMetaDataElement("response-head");
if (/Location:/i.test(headers)) {
cacheEntryDescriptor.close();
return;
}
var contentType = null;
try {
contentType = /Content-Type: *(.*)/i.exec(headers)[1];
} catch (e) {
}
var contentLength = null;
try {
contentLength = /Content-Length: *(.*)/i.exec(headers)[1];
} catch (e) {
}
var contentDisp = null;
try {
contentDisp = /Content-Disposition: *(.*)/i.exec(headers)[1];
} catch (e) {
}
cacheEntryDescriptor.close();
var wnd = this.getWindowFromChannel(channel);
this.analyze(url, contentType, contentDisp, contentLength, wnd);
}
} catch (e) {
//com.VidBar.__e("com.VidBar.NetMonitor.monitorRequest: " + e);
}
},
monitorResponse : function(subject) {
//com.VidBar.__d("com.VidBar.NetMonitor.monitorResponse");
try {
var request = subject
.QueryInterface(Components.interfaces.nsIRequest);
var mediaUrl = request.name;
var httpChannel = subject
.QueryInterface(Components.interfaces.nsIHttpChannel);
var contentType = null;
try {
contentType = httpChannel.getResponseHeader("content-type");
} catch (e) {
}
var contentLength = null;
try {
contentLength = httpChannel.getResponseHeader("content-length");
} catch (e) {
}
var contentDisp = null;
try {
contentDisp = httpChannel
.getResponseHeader("content-disposition");
} catch (e) {
}
this.analyze(mediaUrl, contentType, contentDisp, contentLength,
this.getWindowFromChannel(httpChannel));
} catch (e) {
com.VidBar.__e("com.VidBar.NetMonitor.monitorResponse: " + e);
}
},
analyze : function(mediaUrl, contentType, contentDisp, contentLength, wnd) {
//com.VidBar.__d("com.VidBar.NetMonitor.analyze");
var hit = false;
if ((contentType != null && this.typePattern.test(contentType))
|| (this.reqPattern.test(mediaUrl))) {
if (contentLength != null && isNaN(contentLength) == false
&& contentLength < this.minMediaSize)
return;
var names = this.getFileName(mediaUrl, contentType, contentDisp,
wnd);
var filename = names[0];
var extension = names[1];
try {
var doc = null;
var pageUrl = null;
var Name = null;
var labelFile = filename;
var nameFile = filename;
if (wnd != null && wnd.document) {
doc = wnd.document;
pageUrl = doc.URL;
if (this.pref.getFileNameType() == "title") {
if(doc.title != null) {
nameFile = doc.title;
nameFile = nameFile.replace(/[^`a-zA-Z0-9\-]/g, "_");
if (nameFile.length > 80)
nameFile = nameFile.substring(1,80);
if(nameFile > 25) {
labelFile = nameFile.substring(1,20);
labelFile = labelFile + "..." + extension;
} else
labelFile = nameFile + "." + extension;
nameFile = nameFile +"." + extension;
}
}
}
var data = {
type : "network",
label : labelFile,
filename : nameFile,
extension : extension,
pageUrl : pageUrl,
mediaUrl : mediaUrl,
referrer : pageUrl,
size : contentLength
};
com.VidBar.__d("com.VidBar.NetMonitor.analyze\ndoc: " + pageUrl + "\nfilename: " + filename);
var rep = this.repository;
setTimeout(function() {
rep.addMediaListByDoc([data], doc);
}, 0, null);
} catch (e) {
com.VidBar.__e("com.VidBar.NetMonitor.analyze: " + e);
}
}
},
getFileName : function(mediaUrl, contentType, contentDisp, wnd) {
com.VidBar.__d("com.VidBar.NetMonitor.getFileName");
var filename = null;
var extension = null;
if (contentDisp != null) {
if (/filename=/.test(contentDisp)) {
filename = /filename="?([^;"]*)/.exec(contentDisp)[1];
try {
extension = /.*\.(.*?)$/.exec(filename)[1];
} catch (e) {
extension = "";
}
}
}
if (filename == null) {
if (contentType != null && /^video\/x-.*$/.test(contentType)) {
extension = /video\/x-([^;]*)/.exec(contentType)[1];
} else if (contentType != null && /^video\/.+$/.test(contentType)) {
extension = /video\/([^ ,]*).*$/.exec(contentType)[1];
} else if (contentType != null && /^audio\/.+$/.test(contentType)) {
extension = /audio\/(?:x-)?([^ ,]*).*?$/.exec(contentType)[1];
} else {
if (/^[^\?]*\.[0-9a-zA-Z]{1,5}$/.test(mediaUrl))
extension = /\.([0-9a-zA-Z]{1,5})$/.exec(mediaUrl)[1];
else
extension = "flv";
}
var re = new RegExp("([^/&\\?]+\\." + extension + ")(?:$|&|\\?)");
if (re.test(mediaUrl)) {
var m = re.exec(mediaUrl);
filename = m[1];
} else if (this.reqPattern.test(mediaUrl)) {
var m = this.reqPattern.exec(mediaUrl);
filename = m[1];
extension = m[2];
} else {
try {
var title = null;
if (wnd) {
title = Util.xpGetString(wnd.document.documentElement,
"/html/head/meta[@name='title']/@content");
if (title == null || title == "")
title = Util.xpGetString(
wnd.document.documentElement,
"/html/head/title");
}
if (title == null || title == "")
title = "file-"
+ Math.floor(Math.random() * 1000000000);
filename = title.replace(/[^a-zA-Z0-9-]+/g, "_") + "."
+ extension;
} catch (e) {
filename = "file-" + Math.floor(Math.random() * 1000000000)
+ "." + extension;
}
}
if (filename.length > 64) {
var parts = /^(.*)(\..*?)$/.exec(filename);
filename = parts[1].substr(0, 64 - parts[2].length) + parts[2];
}
}
return [filename, extension];
}
};